Skip to content

Media modal: Show the fields registered for attachments - #86457

Merged
andrewserong merged 3 commits into
WordPress:trunkfrom
padams:add/media-modal-registered-fields
Nov 11, 2026
Merged

andrewserong merged 3 commits into
WordPress:trunkfrom
padams:add/media-modal-registered-fields

Conversation

@padams

@padams padams commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #74865, #75123

The DataViews media modal shows the fields registered for attachments on the server, alongside its own, as columns and filters.

Why?

The modal's fields are a fixed list in MediaUploadModal. A field a plugin registers for postType/attachment with wp_register_entity_fields() shows in the media editor, which reads the registry, but not in the modal. A plugin that adds attachment taxonomies, credits or license fields has no way to let people filter or see them where they pick media.

How?

  • MediaUploadModal takes an optional fields prop. Registered fields are added after the modal's own. A registered field with the ID of one of the modal's own is left out, because those carry behavior specific to the modal: the thumbnail can't be hidden, and "Attached to" has options for the current post. The core attachment collection registers the same IDs, so nothing changes for core's fields.
  • media-utils has no dependency on editor, so the editor's editor.MediaUpload wrapper supplies them, as it already supplies the current post. It reads getEntityFields( 'postType', 'attachment' ) only while the modal is open, so the fields are requested the first time the modal opens, not wherever a media placeholder renders.

The MediaEdit components in fields and media-utils also render the modal and don't pass registered fields yet. That can follow once this approach is agreed.

Testing Instructions

  1. Turn on the "DataViews media modal" experiment in Gutenberg > Experiments.
  2. Add this plugin, which registers an attachment taxonomy and a field for it:
<?php
/*
 * Plugin Name: Media modal field test
 */
add_action( 'init', function () {
	register_taxonomy( 'genre', 'attachment', array(
		'label'        => 'Genres',
		'show_in_rest' => true,
		'rest_base'    => 'genres',
	) );
} );

add_action( 'wp_entity_fields_api_init', function () {
	$terms = get_terms( array( 'taxonomy' => 'genre', 'hide_empty' => false ) );
	wp_register_entity_fields( 'media-modal-field-test', 'postType', 'attachment', array(
		array(
			'id'       => 'genres',
			'type'     => 'array',
			'label'    => 'Genres',
			'elements' => array_map(
				fn( $term ) => array( 'value' => $term->term_id, 'label' => $term->name ),
				is_array( $terms ) ? $terms : array()
			),
			'filterBy' => array( 'operators' => array( 'isAny', 'isAll', 'isNone' ) ),
		),
	) );
} );
  1. Add two terms under Media > Genres, and assign them to a few images on the Edit Media screen.
  2. Open a post, add an Image block and click "Media Library".
  3. Open "Add filter": Genres is offered, with its two terms.
  4. Switch to the table layout and add Genres in the view options: the column shows each image's genres.

With only this PR, the Genres filter shows but doesn't change the results yet: the modal's query doesn't know about taxonomy fields. #86458 does that.

Testing Instructions for Keyboard

The filter and column use DataViews' own controls, so keyboard use is the same as for the modal's other filters: tab to "Add filter", open it with Enter, and choose Genres with the arrow keys.

Use of AI Tools

Written with Claude Code (Claude Opus 5.5) and reviewed by me.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @padams! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: padams <padams@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added [Package] Editor /packages/editor [Package] Media Utils /packages/media-utils First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Nov 6, 2026
@padams
padams marked this pull request as ready for review November 6, 2026 19:57
@ramonjd ramonjd added the [Type] Enhancement A suggestion for improvement. label Nov 9, 2026
@padams
padams force-pushed the add/media-modal-registered-fields branch from dcd6efd to 03d8c57 Compare November 9, 2026 16:33
@andrewserong

Copy link
Copy Markdown
Contributor

Passing in fields to the media upload modal from the editor and for those to be added after the curated ones sounds good to me! This'll just need a rebase now that #86458 has been merged, and then we can look at getting this one in, too.

Thanks again for your help with these!

@padams
padams force-pushed the add/media-modal-registered-fields branch from 03d8c57 to 475774b Compare November 10, 2026 05:53
@padams

padams commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto trunk after #86458. The only conflicts were in the media-utils changelog and the modal's test file, where both PRs added lines in the same spot, so I kept both sets. The code is unchanged, and CI is green on 475774b.

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing nicely for me for the filter use case:

2026-11-11.15.22.09.mp4

Something I noticed while testing (and this also applies to the post-list) is that by default registered fields will show up in the sort by list, but the attachments REST API endpoint doesn't support it by default:

image

A couple of options for plugins:

  • When registering fields, set enableSorting to false, or:
  • Also use the rest_attachment_collection_params and rest_attachment_query filters to make sure the fields are accepted in the orderby value and then passed along appropriately to the query

Something that'll be interesting to do dig into further down the track is how much the fields registration API should handle automatically for attachments so that plugins don't need to do so much plumbing to get things working.

In any case, this'll also be interesting stuff to play with for media folders, too.


Thanks again for the work here, I'll merge it in!

@andrewserong
andrewserong merged commit 44cdffb into WordPress:trunk Nov 11, 2026
85 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 11, 2026
@andrewserong andrewserong added the [Feature] Media Anything that impacts the experience of managing media label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Media Anything that impacts the experience of managing media First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Editor /packages/editor [Package] Media Utils /packages/media-utils [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants